﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeTableManagement.Entities;
using TimeTableManagement;

namespace TimeTableManagement.Controllers
{
    [Authorize]
    public class SlotController : Controller
    {
        private QLSVContext db = new QLSVContext();

        //
        // GET: /Slot/

        public ViewResult Index()
        {
            return View(db.Tb_Slot.ToList());
        }

        //
        // GET: /Slot/Details/5

        public ViewResult Details(int id)
        {
            Tb_Slot tb_slot = db.Tb_Slot.Find(id);
            return View(tb_slot);
        }
        public ViewResult Details2(int id)
        {
            Tb_Slot tb_slot = db.Tb_Slot.Find(id);
            return View(tb_slot);
        }
        //
        // GET: /Slot/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Slot/Create

        [HttpPost]
        public ActionResult Create(Tb_Slot tb_slot)
        {
            if (ModelState.IsValid)
            {
                db.Tb_Slot.Add(tb_slot);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tb_slot);
        }
        
        private bool checkfromto(Tb_Slot tb_slot)
        {
            int? _from = tb_slot.from;
            int? _to = tb_slot.to;
            var allslot = db.Tb_Slot.ToList();
            foreach (Tb_Slot item in allslot)
            {
                if (item.from <= _from && _from < item.to)
                {
                    return false;
                }
            }
            return true;
        }
        //
        // GET: /Slot/Delete/5
 
        public ActionResult Delete(int id)
        {
            Tb_Slot tb_slot = db.Tb_Slot.Find(id);
            return View(tb_slot);
        }

        //
        // POST: /Slot/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Tb_Slot tb_slot = db.Tb_Slot.Find(id);
            db.Tb_Slot.Remove(tb_slot);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}